package aom.ju.ss.view.cut

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.view.ViewCompat
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.CutGoodsDetailsBinding
import aom.ju.ss.mode.GoodsInfo
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.get_info
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.dialog.CxAttrProductModelDialog
import aom.ju.ss.view.dialog.EnsureDialog
import aom.ju.ss.view.dialog.SharePromotionDialogLp
import aom.ju.ss.view.shua.ShopDetailsActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.google.gson.Gson

/**
 * 砍价商品详情  品牌商品
 */
class CutGoodsDetailsActivity : BaseActivity() {
    private lateinit var binding: CutGoodsDetailsBinding
    override fun onBackPressed() {
        finishAfterTransition()
    }

    override fun init() {
        binding = viewBinding as CutGoodsDetailsBinding

        Glide.with(this)
            .load(intent.getStringExtra("img"))
            .centerCrop()
            .into(binding.img)
        binding.back.setOnClickListener { finishAfterTransition() }
        binding.share.setOnClickListener { SharePromotionDialogLp(0, activity!!, null) }

        binding.yf.setOnClickListener { }
        binding.bz.setOnClickListener { EnsureDialog(activity!!) }
        OKHttpUtils(activity, false)
            .SetApiUrl(get_info)
            .SetKey("id")
            .SetValue(intent.getStringExtra("data"))
            .POST(this)

        binding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                binding.num.text =
                    (binding.vp.currentItem + 1).toString() + "/" + binding.vp.adapter!!.count.toString()
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        binding.pay.VG(false)
        binding.addCar.VG(false)
        binding.line.VG(false)


    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        if (httpTY == get_info) {
            val mode = Gson().fromJson(jsonString, GoodsInfo::class.java)
            binding.info = mode.data
            binding.yj.zhx()
            mode.data.images?.let {
                val banns = arrayListOf<String>()
                val dets = arrayListOf<String>()
                for (a in it) {
                    if (a.type == 1) {
                        if (a.image.length > 1) {
                            banns.add(a.image)
                        }
                    } else {
                        dets.add(a.image)
                    }
                }
                showListImg().loadImg(activity!!, binding.picLay, dets)

                binding.vp.show(banns, supportFragmentManager)
                binding.num.text =
                    (binding.vp.currentItem + 1).toString() + "/" + binding.vp.adapter!!.count.toString()
                binding.img.VG(false)
            }


            binding.cutPay.setOnClickListener {
                if (mode.data.skus == null || mode.data.skus!!.size == 0) {
                    "商品无规格,无法购买".show
                    return@setOnClickListener
                }
                CxAttrProductModelDialog(mode.data, activity!!)
            }

            binding.yx.setOnClickListener { binding.cutPay.performClick() }
//类型  1普通商品  2砍价商品  3抢券商品  4易货商品
            when (mode.data.type) {
                1 -> {//品牌
                }
                2 -> {//砍价
                }
                3 -> {//3抢券商品

                }
            }
            binding.pay.setOnClickListener {
                binding.cutPay.performClick()
            }
            binding.addCar.setOnClickListener {
                if (mode.data.skus == null || mode.data.skus!!.size == 0) {
                    "商品无规格,无法购买".show
                    return@setOnClickListener
                }
                CxAttrProductModelDialog(mode.data, activity!!, true)
            }

            binding.pd.setOnClickListener {
                start(ShopDetailsActivity::class.java, mode.data.shop_id.toString())
            }
            binding.kf.setOnClickListener { "正在努力开发中...".show }
            binding.sc.setOnClickListener { "正在努力开发中...".show }
        }

    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
    }

    override val lay: Int = R.layout.cut_goods_details


    /**
     * 显示详情图片
     */
    class showListImg {
        var p = 0
        fun loadImg(context: Context, linearLayout: LinearLayout, list: List<String>) {
            if (list.isEmpty()) return
            Glide.with(context)
                .asBitmap()
                .load(list[p])
                .centerCrop()
                .into(object : CustomTarget<Bitmap>(
                    SIZE_ORIGINAL, SIZE_ORIGINAL
                ) {
                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        super.onLoadFailed(errorDrawable)
                        if (p + 1 < list.size) {
                            p++
                            loadImg(context, linearLayout, list)
                        }
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {}
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        val width = BaseApplication.getInstance().w.toFloat()
                        val scale = width / resource.width
                        val afterWidth = (resource.width * scale).toInt()
                        val afterHeight = (resource.height * scale).toInt()
                        val layoutInflater = LayoutInflater.from(context)
                        val mView = layoutInflater.inflate(R.layout.view_productdetail_layout, null)
                        val imageViewNew = mView.findViewById<ImageView>(R.id.iv_img)
                        val lp = imageViewNew.layoutParams as ViewGroup.MarginLayoutParams
                        lp.width = afterWidth
                        lp.height = afterHeight
                        imageViewNew.layoutParams = lp
                        imageViewNew.scaleType = ImageView.ScaleType.FIT_XY
                        imageViewNew.setImageBitmap(resource)
                        val params = LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                        )
                        linearLayout.addView(mView, params)
                        if (p + 1 < list.size) {
                            p++
                            loadImg(context, linearLayout, list)
                        }
                    }
                })
        }

    }
}